#include<bits/stdc++.h>
#define int long long
#define MOD 998244353
#define REP(i,a,n) for(int i=(a);i<(int)(n);++i)
#define pb push_back
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define deal(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define lbound(v,x) (lower_bound(all(v),x)-v.begin())
using namespace std;
mt19937 sd(random_device{}());
int qpow(int a,int b,int m=MOD){
    int res=1;
    while(b){
        if(b&1)res=res*a%m;
        a=a*a%m;b>>=1;
    }
    return res;
}
int read(){
    int res=0;char c=getchar();
    while(c<48||c>57)c=getchar();
    do res=(res<<1)+(res<<3)+(c^48),c=getchar();while(c>=48&&c<=57);
    return res;
}
int n;
int a[200005];
int f[200005];
int p[200005];
int g[1000005];
void Main(){
    cin>>n;
    REP(i,0,n)cin>>a[i];
    f[1]=0;
    REP(i,2,n)f[i]=f[i-1]+(a[i-1]==a[i-2]? a[i-1]:0);
    p[0]=0;
    REP(i,1,n)p[i]=p[i-1]+(a[i]==a[i-1]? a[i]:0);
    REP(i,0,n)g[a[i]]=-1e18;
    int mx=-1e18;
    REP(i,2,n){
        f[i]=max(f[i],f[i-1]+(a[i]==a[i-2]? a[i]:0));
        mx=max(mx,f[i-2]-p[i-2]);
        f[i]=max(f[i],mx+p[i-1]);
        if(i>2){
            g[a[i-3]]=max(g[a[i-3]],f[i-2]-p[i-2]);
        }
        f[i]=max(f[i],g[a[i]]+a[i]+p[i-1]);
    }
    int ans=f[n-1],s=0;
    for(int i=n-2;i;--i){
        s+=a[i]==a[i+1]? a[i]:0;
        ans=max(ans,f[i]+s);
    }
    s+=a[0]==a[1]? a[0]:0;
    cout<<max(s,ans)<<endl;
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int tc=1;
    cin>>tc;
    while(tc--)Main();
    return 0;
}